## try to generate string like 'c(a1, a2, a3) + c(b1, b2, b3)'

order_generator <- function(
  parts = list(),
  sep_char = "\n"
){
  final_output <- ""
  times <- 1
  for(i in parts){
    length_temp <- length(i)
    if(length_temp > times){
      times <- length_temp
    }
  }
  # cat("Loop ", times," times in this order.\n")
  for(i in 1:times){
    for(j in parts){
      if(length(j) <= i){
        final_output <- paste(final_output, j[length(j)], sep = "")
      }else{
        final_output <- paste(final_output, j[i], sep = "")
      }
    }
    if(i == times){
      final_output <- paste(final_output, sep = "")
    }else{
      final_output <- paste(final_output, sep_char, sep = "")
    }
  }
  return(final_output)
}

# setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# source("list_picker.R")

order_generator2 <- function(
  parts = list(),
  parts_vec = c(),
  sep_vec = c()
){
  final_output <- ""
  for(i in unique(parts_vec)){
    if(length(parts_vec[parts_vec == i]) > 1){
      final_output <- paste(final_output, 
                            order_generator(list_picker(ori_list = parts,
                                                        pick_vec = grep(i, parts_vec)
                                                        ),
                                            sep_char = sep_vec[i]),
                            sep = "")
    }else{
      final_output <- paste(final_output, parts[[grep(i, parts_vec)]], sep = "")
    }
  }
  return(final_output)
}


####Testing Area####
# cat(order_generator(
#   list(
#     "col",
#     1:4,
#     "_vec[j] <- strsplit(ori_data[j], \",\")[[1]][",
#     1:4,
#     "]"
#   )
# ))test

# test<- order_generator2(
#   parts = list(
#     "#This is head test\n\n",
#     "for(i in 1:dim(ori_data)[1]){
#   if(
#     (grepl(\".\",c(ori_data$C",
#     2:4,
#     "[i]), fixed = T) || 
#      grepl(\"e\",c(ori_data$C",
#     2:4,
#     "[i]), fixed = T))&
#     grepl(\"(\",c(ori_data$C",
#     2:4,
#     "[i]), fixed = T) == F &
#     grepl(\")\",c(ori_data$C",
#     2:4,
#     "[i]), fixed = T) == F
#   ){}",
#     "# This is the No.",
#     1:3,
#     "tail.\n\n"
#   ),
#   parts_vec = c(1, 2,2,2,2,2,2,2,2,2,3,3,3),
#   sep_vec = c("this will not show\n",
#                "I am the second sep \n",
#                "I am the thrid sep\n")
# )
# cat(test)
# order = ""
# order <- order_generator2(
#   parts = list(
#     "ori_data <- data.frame(",
#     "nc",
#     1:4,
#     ")"
#   ),
#   parts_vec = c(1,2,2,3),
#   sep_vec = c("", ",", "")
# )
# cat(order)